package com.tomecode.tools.maven.settings.gui.frm;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.grouplayout.GroupLayout;
import org.eclipse.swt.layout.grouplayout.LayoutStyle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;

import com.tomecode.tools.maven.settings.gui.images.ImageFactory;
import com.tomecode.tools.maven.settings.gui.utils.GuiUtils;
import com.tomecode.tools.maven.settings.model.Profile;

/**
 * Dialog for selecting profile
 * 
 * @author Tomas Frastia
 * @see http://www.tomecode.com or
 *      http://code.google.com/p/maven-settings-editor/
 * 
 */
public final class FrmSelectProfile extends Dialog {

	private String result;
	private Shell shell;
	private java.util.List<Profile> profiles;
	private java.util.List<String> activeProfiles;
	private List listActiveProfiles;

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public FrmSelectProfile(Shell parent, java.util.List<Profile> profiles, java.util.List<String> activeProfiles) {
		super(parent, SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
		setText("Select Active Profile");
		this.activeProfiles = activeProfiles;
		this.profiles = profiles;
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public final String open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private final void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setImage(ImageFactory.MAVEN_SETTINGS_EDITOR_16);
		shell.setSize(450, 382);
		shell.setText(getText());

		Composite composite = new Composite(shell, SWT.NONE);
		if (GuiUtils.isWindows()) {
			composite.setBounds(0, 0, 444, 275);
		}

		Button btnCancel = new Button(composite, SWT.NONE);
		if (GuiUtils.isWindows()) {
			btnCancel.setBounds(340, 238, 94, 30);
		}
		btnCancel.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				result = null;
				shell.close();
			}
		});
		btnCancel.setText("Cancel");

		final Button btnSelect = new Button(composite, SWT.NONE);
		if (GuiUtils.isWindows()) {
			btnSelect.setBounds(240, 238, 94, 30);
		}
		btnSelect.setEnabled(false);
		btnSelect.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				shell.close();
			}
		});
		btnSelect.setText("Select");

		Group group = new Group(composite, SWT.NONE);
		if (GuiUtils.isWindows()) {
			group.setBounds(0, 0, 444, 233);
		}

		listActiveProfiles = new List(group, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
		fillList();

		listActiveProfiles.addMouseListener(new MouseAdapter() {
			public final void mouseDoubleClick(MouseEvent e) {
				int index = listActiveProfiles.getSelectionIndex();
				if (index != -1) {
					String item = listActiveProfiles.getItem(index);

					if (activeProfiles.contains(item)) {
						btnSelect.setEnabled(false);
						result = null;
					} else {
						btnSelect.setEnabled(true);
						result = item;
						shell.close();
					}
				}
			}
		});
		listActiveProfiles.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				int index = listActiveProfiles.getSelectionIndex();
				btnSelect.setEnabled(index != -1);

				if (listActiveProfiles.getSelectionIndex() != -1) {
					String item = listActiveProfiles.getItem(index);
					if (activeProfiles.contains(item)) {
						btnSelect.setEnabled(false);
						result = null;
					} else {
						btnSelect.setEnabled(true);
						result = item;
					}
				}

			}
		});
		GroupLayout gl_shell = new GroupLayout(shell);
		gl_shell.setHorizontalGroup(gl_shell.createParallelGroup(GroupLayout.LEADING).add(composite, GroupLayout.DEFAULT_SIZE, 444, Short.MAX_VALUE));
		gl_shell.setVerticalGroup(gl_shell.createParallelGroup(GroupLayout.LEADING).add(composite, GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE));
		GroupLayout gl_composite = new GroupLayout(composite);
		gl_composite.setHorizontalGroup(gl_composite.createParallelGroup(GroupLayout.LEADING)
				.add(gl_composite.createSequentialGroup().add(242).add(btnSelect, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE).addPreferredGap(LayoutStyle.RELATED).add(btnCancel, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE).addContainerGap())
				.add(group, GroupLayout.DEFAULT_SIZE, 444, Short.MAX_VALUE));
		gl_composite.setVerticalGroup(gl_composite.createParallelGroup(GroupLayout.LEADING).add(
				gl_composite.createSequentialGroup().add(group, GroupLayout.PREFERRED_SIZE, 309, GroupLayout.PREFERRED_SIZE).addPreferredGap(LayoutStyle.UNRELATED)
						.add(gl_composite.createParallelGroup(GroupLayout.BASELINE).add(btnCancel, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE).add(btnSelect, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)).addContainerGap()));
		GroupLayout gl_group = new GroupLayout(group);
		gl_group.setHorizontalGroup(gl_group.createParallelGroup(GroupLayout.TRAILING).add(GroupLayout.LEADING, listActiveProfiles, GroupLayout.DEFAULT_SIZE, 436, Short.MAX_VALUE));
		gl_group.setVerticalGroup(gl_group.createParallelGroup(GroupLayout.TRAILING).add(GroupLayout.LEADING, listActiveProfiles, GroupLayout.DEFAULT_SIZE, 295, Short.MAX_VALUE));
		group.setLayout(gl_group);
		composite.setLayout(gl_composite);
		shell.setLayout(gl_shell);
		if (GuiUtils.isWindows()) {
			listActiveProfiles.setBounds(10, 10, 424, 218);// 228);
		}
	}

	private final void fillList() {
		for (Profile profile : profiles) {
			if (!activeProfiles.contains(profile.getId())) {
				listActiveProfiles.add(profile.getId());
			}
		}
	}
}
